<template>
  <div>
    <pro-table
      ref="tableRef"
      :data="tableData"
      :total="total"
      :columns="columns"
      :tool-button="false"
      @search="commEvent.onSearch"
      :page-change="commEvent.onPageChange"
    >
      <template #tableHeader>
        <el-row justify="end" v-auth="['sys:user:save']">
          <el-button type="primary" icon="Plus" @click="commEvent.onSave()">新增</el-button>
        </el-row>
      </template>
      <template #operation="{ row }">
        <el-button type="primary" link @click="commEvent.onSave(row)" v-auth="['sys:user:save']">编辑</el-button>
        <el-button type="danger" link @click="commEvent.onDelete(row)" v-auth="['sys:user:remove']">删除</el-button>
      </template>
    </pro-table>
    <!-- 新增编辑弹窗 -->
    <save-user v-model="saveVisible" :row="currentUser" @refresh="commEvent.onSearch" />
  </div>
</template>

<script setup lang="ts">
import { h, onMounted, ref } from "vue"
import ProTable from "@/components/ProTable/index.vue"
import SaveUser from "./components/SaveUser.vue"
import { ColumnProps, ProTableInstance } from "@/components/ProTable/interface"
import { ElMessage, ElMessageBox } from "element-plus"
import { statusOptions } from "@/utils/dict"
import { getUserPageListApi, deleteUserApi } from "@/api/modules/system"
import { SystemSpace } from "@/api/interface/system"
// 参数
const tableRef = ref<ProTableInstance>() // 表格实例
const tableData = ref<SystemSpace.UserPageList[]>([]) // 用户信息
const total = ref<number>(0) // 总数
const saveVisible = ref<boolean>(false) // 新增编辑弹窗
const currentUser = ref<SystemSpace.UserPageList>() // 当前用户信息
// 页码参数
const pageParams = ref({
  pageIndex: 1,
  pageSize: 10
})
// 表格配置
const columns: ColumnProps<any>[] = [
  {
    prop: "sort",
    label: "序号",
    width: 80,
    render: scope => h("span", scope.$index + 1 + (pageParams.value.pageIndex - 1) * pageParams.value.pageSize)
  },
  { prop: "name", label: "用户名称", search: { el: "input" } },
  { prop: "account", label: "用户账号", search: { el: "input" } },
  { prop: "status", label: "用户状态", enum: statusOptions, width: 180, search: { el: "select" } },
  { prop: "phone", label: "手机号码", width: 200, search: { el: "input" } },
  { prop: "email", label: "电子邮箱", width: 300, search: { el: "input" } },
  { prop: "operation", label: "操作", width: 120, fixed: "right" }
]
// 事件集合
const commEvent = {
  // 获取用户数据
  getUsers: async (params: SystemSpace.UserPageListQuery) => {
    const { data } = await getUserPageListApi(params)
    tableData.value = data.result
    total.value = data.totalCount
  },
  // 查询
  onSearch: () => {
    tableRef.value?.search()
    pageParams.value.pageIndex = 1
    const _search = tableRef.value?.searchParam
    commEvent.getUsers({ ...pageParams.value, ..._search })
  },
  // 分页
  onPageChange: (page: number, size: number) => {
    pageParams.value.pageIndex = page
    pageParams.value.pageSize = size
    const _search = tableRef.value?.searchParam
    commEvent.getUsers({ ...pageParams.value, ..._search })
  },
  // 新增/编辑
  onSave: (row?: SystemSpace.UserPageList) => {
    currentUser.value = row
    saveVisible.value = true
  },
  // 删除
  onDelete: (row: SystemSpace.UserPageList) => {
    ElMessageBox.confirm("确定要删除吗?", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning"
    })
      .then(async () => {
        const res = await deleteUserApi({ id: row.id })
        if (res.code === "200") {
          ElMessage.success("删除成功")
          commEvent.onSearch()
        }
      })
      .catch(() => {})
  }
}
onMounted(() => {
  commEvent.getUsers(pageParams.value)
})
</script>

<style lang="scss" scoped></style>
